﻿; This script wraps the commonly used win32api

#Requires AutoHotkey v2.0

GetWindowRectWithoutShadow(hwnd, &x, &y, &w, &h) {
  rect := Buffer(16)
  DllCall('dwmapi\DwmGetWindowAttribute'
    , 'ptr', hwnd
    , 'uint', 0x9
    , 'ptr', rect
    , 'int', rect.Size)
  top := NumGet(rect, 4, 'int')         ; top：矩形顶部的y坐标
  left := NumGet(rect, 'int')           ; left：矩形左侧的x坐标
  right := NumGet(rect, 8, 'int')       ; right：矩形右侧的x坐标
  bottom := NumGet(rect, 12, 'int')     ; bottom：矩形底部的y坐标

  x := left
  y := top
  w := right - left
  h := bottom - top
}

GetClipboardData() {
  ; 有时无法获取到数据
  if !DllCall('OpenClipboard') {
    return '[error] can not open clipboard'
  }
  handle := DllCall('GetClipboardData', 'int', 1) ; CF_TEXT
  if !handle {
    return '[error] can not retrieve data'
  }
  ptr := DllCall('GlobalLock', 'ptr', handle)
  data := StrGet(ptr, , '')
  DllCall('GlobalUnlock', 'ptr', handle)
  DllCall('CloseClipboard')
  return data
}

ClipboardGet_HTML() { ; http://www.autohotkey.com/forum/viewtopic.php?p=392624#392624
  if CBID := DllCall("RegisterClipboardFormat", 'str', "HTML Format", 'uint') {
    if DllCall("IsClipboardFormatAvailable", 'uint', CBID) != 0 {
      if DllCall("OpenClipboard", 'uint', 0) != 0 {
        if hData := DllCall("GetClipboardData", 'uint', CBID, 'uint') {
          DataL := DllCall("GlobalSize", 'uint', hData, 'uint')
          pData := DllCall("GlobalLock", 'uint', hData, 'uint')
          Data := StrGet(pData, dataL, "UTF-8")
          DllCall("GlobalUnlock", 'uint', hData)
        }
      }
    }
  }
  DllCall("CloseClipboard")
  return dataL ? Data : 0
}